package org.lightmtv.util;

import java.util.HashMap;
import java.util.Map;

import org.lightcommons.template.Template;
import org.lightcommons.util.Assert;
import org.lightcommons.web.auth.AuthManager;
import org.lightmtv.LightMtvException;
import org.lightmtv.config.GlobalContext;
import org.lightmtv.config.Settings;
import org.lightmtv.response.HTML;

/**
 * 
 * @author GL
 * @deprecated use TemplateResponse instead.
 */
@Deprecated
public class Renderer{
	private Template template;
	private Map map;
	public Renderer(Template template) {
		super();
		Assert.notNull(template,"templat must not be null");
		this.template = template;
		initVars();
	}
	
	public Renderer(String path){
		Settings settings=GlobalContext.getSettings();
		try {
			this.template = settings.getTemplateFactory().getTemplate(path, settings.getEncoding());
		} catch (Exception e) {
			throw new LightMtvException("Unable to initilize Template of '"+path+"'",e);
		}
		Assert.notNull(this.template,"Template not found: "+path);
		initVars();
	}
	
	private void initVars(){
		map = new HashMap();
		map.put("global", GlobalContext.getVars());
		map.put("req", GlobalContext.getRequest());
		map.put("auth", AuthManager.getAuthentication());
		map.put("loginUser", AuthManager.getUser());
	}
	
	public Renderer put(Object key,Object value){
		map.put(key, value);
		return this;
	}

	public HTML asHTML(){
		return new HTML(template.render(map));
	}
}
